const userService = require('../service/user.service')
const { UPLOAD_PATH } = require('../config/path')
const fs = require('fs')
class UserController {
    async create(ctx) {
        // 1.获取用户传递过来的信息
        const user = ctx.request.body
        // console.log(user)
        // 3.查看存储的结果，告知前端创建工作
        const result = await userService.create(user)
        ctx.body = {
            code: 0,
            message: '创建用户成功',
            data: result
        }
    }
    async showAvatarImage(ctx) {
        // 1拿到上传头像的用户ID
        const { userId } = ctx.params
        // 2.获取userid的头像信息
        const avatarInfo = await userService.findAvatarById(userId)
        // 3.读取头像所在文件
        const { filename, mimetype } = avatarInfo
        ctx.type = mimetype

        ctx.body = fs.createReadStream(`${UPLOAD_PATH}/${filename}`)
        // ctx.body = {
        //     code: 0,
        //     message: '查找头像成功~',
        //     data: avatarInfo
        // }
    }
    // 删除用户
    async remove(ctx) {
        const result = await userService.delete(ctx.params.id);
        ctx.body = result;
    }
    // 更新用户
    async update(ctx) {
        const result = await userService.update(
            ctx.params.id,
            ctx.request.body
        );
        ctx.body = result;
    }
    // 查看用户数据库单条数据
    async get(ctx) {
        const result = await userService.get(ctx.params.id);
        ctx.body = result;
    }

    async getList(ctx) {
        const result = await userService.getList(ctx.request.body);
        ctx.body = result;
    }

}
module.exports = new UserController() 